package com.yuntu.controller;

import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;
import com.yuntu.pojo.Dev_User;
import com.yuntu.service.Dev_UserService;
import com.yuntu.utils.PageUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/dev")
public class Dev_UserController {
    private Logger logger=Logger.getLogger(Dev_UserController.class);

    @Autowired
    private Dev_UserService d_Service;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(@RequestParam String devCode, @RequestParam String devPassword,HttpSession session){
        logger.debug("----------------------进入登录");
        Dev_User user=d_Service.login(devCode,devPassword);
        if (user!=null){
            session.setAttribute("userSession",user);
            return "redirect:/dev/show_All";
        }else {
            //异常信息发送给登录页
            session.setAttribute("error","用户或者密码不正确");
            int i=1/0;
            //System.out.println("老子就是不想让你登录");
            return "login";
        }
    }

    /*分页显示*/
    @RequestMapping(value = "/show_All",method = RequestMethod.GET)
    public String show_All(Integer pageIndex, HttpSession session){
        System.out.println("-------------getPage");
        //设置初始
        if (pageIndex==null){
            pageIndex=1;
        }
        PageUtil<Dev_User> pageUtil=new PageUtil<>();
        pageUtil.setPageindex(pageIndex);
        pageUtil.setPagesize(2);
        d_Service.getPageDev_UserList(pageUtil);
        session.setAttribute("pageUtil",pageUtil);
        return "frame";
    }

    @RequestMapping(value = "/add.do",method = RequestMethod.GET)
    public String add_do(){
        return "add";
    }

    @RequestMapping(value = "/addDev_User",method = RequestMethod.GET)
    public String add_peitems(Dev_User dev_user){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dev_user.setCreationDate(df.format(new Date()));
        if (d_Service.addDev_User(dev_user)){
            return "redirect:/dev/show_All";
        }
        return "add";
    }

    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public  String delete(@PathVariable int id){
        if (d_Service.del_id(id)){
            return "redirect:/dev/show_All";
        }else{
            return "redirect:/dev/show_All";
        }
    }

    @RequestMapping(value = "/selone/{itemid}",method = RequestMethod.GET)
    public String selOne(@PathVariable int itemid, HttpSession session){
        Dev_User p = d_Service.selone(itemid);
        if (p!=null){
            session.setAttribute("p",p);
            return "upda";
        }
        return "frame";
    }

    @RequestMapping(value = "/update/{id}",method = RequestMethod.GET)
    public String update(Dev_User friend,@PathVariable int id){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        friend.setModifyDate(df.format(new Date()));
        if (d_Service.update(friend)){
            return "redirect:/dev/show_All";
        }else{
            return "upda";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/show_name",method = RequestMethod.GET)
    public Object show_name(@RequestParam String devName){
        Map<String,String> map=new HashMap<>();
        if(StringUtils.isNullOrEmpty(devName)){
            map.put("devName","exist");
        }else {
            Dev_User user=d_Service.shwo_name(devName);
            if(user!=null){
                map.put("devName","exist");
            }else {
                map.put("devName","noexist");
            }
        }
        return JSONArray.toJSONString(map);
    }
}
